﻿using Microsoft.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 图书管理.Model;
using Dapper;

namespace 图书管理.Services
{
    internal class BookTypeUtils
    {
        private static readonly string constr = "Data Source=.;Initial Catalog=books;User Id=sa;Password=root123;Encrypt=False";

        internal IEnumerable<BookTyple> SelectAll()
        {
            using (var connection = new SqlConnection(constr))
            {
                var bookTypes = connection.Query<BookTyple>("SELECT id,name FROM bookType order by id asc ");
                return bookTypes;
            }
        }

        internal bool EditBookType(int selId, string name)
        {
            bool b = false;

            BookTyple bookType = new BookTyple()
            {
                Id = selId,
                Name = name,

            };

            using (var connection = new SqlConnection(constr))
            {
                int v = connection.Execute("UPDATE bookType SET name = @name WHERE Id = @Id", bookType);
                b = v > 0;
            }
            return b;
        }

        internal bool DeleteBookType(int selId)
        {
            bool b = false;

            using (var connection = new SqlConnection(constr))
            {
                int v = connection.Execute("DELETE FROM bookType WHERE Id = @Id", new { Id = selId });
                b = v > 0;
            }
            return b;
        }

        internal bool InsertBookType(string name)
        {
            bool b = false;

            BookTyple person = new BookTyple()
            {
                Name = name
            };

            using (var connection = new SqlConnection(constr))
            {
                int v = connection.Execute("INSERT INTO bookType (name) VALUES (@name)", person);
                b = v > 0;
            }
            return b;

        }
    }
}
